package com.example.administrator.fagementwc;

import android.graphics.Color;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentTransaction;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

public class MainActivity extends AppCompatActivity implements View.OnClickListener {

    private LinearLayout btnChat;
    private LinearLayout  btnContact;
    private LinearLayout  btnFind;
    private LinearLayout  btnMe;

    private ImageView ivChat;
    private ImageView ivContact;
    private ImageView ivFind;
    private ImageView ivMe;

    private TextView tvChat;
    private TextView tvContact;
    private TextView tvFind;
    private TextView tvMe;

    private Fragment chaf = null;
    private Fragment conf = null;
    private Fragment finf = null;
    private Fragment mef = null;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        initView();
        initEvent();
        setTable(0);
    }




    public void initEvent() {
        btnChat.setOnClickListener(this);
        btnContact.setOnClickListener(this);
        btnFind.setOnClickListener(this);
        btnMe.setOnClickListener(this);
    }

    public void initView() {

        //底部按钮
        btnChat = (LinearLayout) findViewById(R.id.id_lay_chat);
        btnContact = (LinearLayout) findViewById(R.id.id_lay_contact);
        btnFind = (LinearLayout) findViewById(R.id.id_lay_find);
        btnMe = (LinearLayout) findViewById(R.id.id_lay_me);

        ivChat = (ImageView) findViewById(R.id.ivChat);
        ivContact = (ImageView) findViewById(R.id.ivContact);
        ivFind = (ImageView) findViewById(R.id.ivFind);
        ivMe = (ImageView) findViewById(R.id.ivMe);

        tvChat = (TextView) findViewById(R.id.tvChat);
        tvContact = (TextView) findViewById(R.id.tvContact);
        tvFind = (TextView) findViewById(R.id.tvFind);
        tvMe = (TextView) findViewById(R.id.tvMe);

    }

    @Override
    public void onClick(View v) {
        resetImage();
        int color = Color.parseColor("#1B940A");
        switch (v.getId()) {
            case R.id.id_lay_chat:
                ivChat.setImageResource(R.drawable.weixin_pressed);
                tvChat.setTextColor(color);
                setTable(0);
                break;
            case R.id.id_lay_contact:
                ivContact.setImageResource(R.drawable.contact_list_pressed);
                tvContact.setTextColor(color);
                setTable(1);
                break;
            case R.id.id_lay_find:
                ivFind.setImageResource(R.drawable.find_pressed);
                tvFind.setTextColor(color);
                setTable(2);
                break;
            case R.id.id_lay_me:
                ivMe.setImageResource(R.drawable.profile_pressed);
                tvMe.setTextColor(color);
                setTable(3);
                break;
            default:
                break;
        }
    }

    public void resetImage() {
        int color = Color.parseColor("#7f7f7f");
        ivChat.setImageResource(R.drawable.weixin_normal);
        tvChat.setTextColor(color);

        ivContact.setImageResource(R.drawable.contact_list_normal);
        tvContact.setTextColor(color);

        ivFind.setImageResource(R.drawable.find_normal);
        tvFind.setTextColor(color);

        ivMe.setImageResource(R.drawable.profile_normal);
        tvMe.setTextColor(color);
    }

    public void setTable(int index) {
        FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
        if (chaf != null) {
            transaction.hide(chaf);
        }
        if (conf != null) {
            transaction.hide(conf);
        }
        if (finf != null) {
            transaction.hide(finf);
        }
        if (mef != null) {
            transaction.hide(mef);
        }
        switch (index) {
            case 0 :
                if (chaf == null) {
                    chaf = new ChatFragment();
                    transaction.add(R.id.MainTab, chaf);
                }else {
                    transaction.show(chaf);
                }
                break;
            case 1 :
                if (conf == null) {
                    conf = new ContactFragment();
                    transaction.add(R.id.MainTab, conf);
                }else {
                    transaction.show(conf);
                }
                break;
            case 2 :
                if (finf == null) {
                    finf = new FindFragment();
                    transaction.add(R.id.MainTab, finf);
                }else {
                    transaction.show(finf);
                }
                break;
            case 3 :
                if (mef == null) {
                    mef = new MeFragment();
                    transaction.add(R.id.MainTab, mef);
                }else {
                    transaction.show(mef);
                }
                break;
            default:
                break;
        }
        transaction.commit();
    }
}
